package com.itcast.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itcast.constant.MessageConstant;
import com.itcast.dao.MemberDao;
import com.itcast.dao.OrderDao;
import com.itcast.dao.OrdersettingDao;
import com.itcast.dao.SetmealDao;
import com.itcast.entity.Result;
import com.itcast.pojo.Member;
import com.itcast.pojo.Order;
import com.itcast.pojo.OrderSetting;
import com.itcast.pojo.Setmeal;
import com.itcast.service.OrderServcie;
import com.itcast.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Service(interfaceClass = OrderServcie.class)
@Transactional
public class OrderServiceImpl implements OrderServcie {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrdersettingDao ordersettingDao;

    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private MemberDao memberDao;

    /**
     * 预约操作
     * @param map
     * @return
     * @throws Exception
     */
    @Override
    public Result submit(Map map) throws Exception {
        //判断预约的日期是否可以预约
        String orderDate = (String) map.get("orderDate");
        Date order_Date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSettingByDate = ordersettingDao.findOrderSettingByDate(order_Date);
        if (orderSettingByDate==null){
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //判断如约人数是否已满
        OrderSetting ordersetting = ordersettingDao.findOrderSettingByDate(order_Date);
        int reservations = ordersetting.getReservations();
        int number = ordersetting.getNumber();
        if (reservations>=number){
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //判断用户是否是会员，不是则自动注册
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findMemberByPhoneNumber(telephone);

        if (member==null){
            member = new Member();
            member.setName((String) map.get("name"));
            member.setIdCard((String) map.get("idCard"));
            member.setPhoneNumber(telephone);
            member.setRegTime(order_Date);
            member.setSex((String) map.get("sex"));
            memberDao.addMember(member);
        }
            Integer memberId = member.getId();
        //判断一人是否在同一天预约了统一套餐
        int setmealId = Integer.parseInt((String) map.get("setmealId"));
        Order order = new Order(memberId, order_Date,setmealId );
        Order ordermune=orderDao.findByOrder(order);
        if (ordermune!=null){
            return new Result(false,MessageConstant.ORDER_FAIL);
        }

//        预约,修改预约人数

        order.setOrderType(Order.ORDERTYPE_TELEPHONE);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        orderDao.addOrder(order);
        OrderSetting orderSetting = ordersettingDao.findOrderSettingByDate(order_Date);
        orderSetting.setReservations(orderSetting.getReservations()+1);
        ordersettingDao.updateByOrderDate(orderSetting);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order);
    }


    /**
     * 查询预约成功信息
     * @param
     * @return
     */
    @Override
    public Map findById(Integer id) {
        Map map = new HashMap();
        Order order = orderDao.findById(id);
        if (order!=null){
            Integer setmealId = order.getSetmealId();
            if (setmealId!=null){
                Setmeal setmeal = setmealDao.findById(setmealId);
                if (setmeal!=null){
                    Member member=memberDao.findById(order.getMemberId());
                    if (member!=null){
                    map.put("orderDate",new SimpleDateFormat("yyyy-MM-dd").format(order.getOrderDate()));
                    map.put("orderType",order.getOrderType());
                    map.put("setmeal",setmeal.getName());
                    map.put("member",member.getName());
                    }
                }
            }
        }
        return map;
    }
}
